package com.sdwz.library.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.base.Strings;
import com.sdwz.commonutil.Result;
import com.sdwz.library.entity.BookInfo;
import com.sdwz.library.entity.vo.BookInfoVo;
import com.sdwz.library.service.BookInfoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api/library/book")
public class BookApiController {

    @Autowired
    BookInfoService bookInfoService;

    /**
     * 通过书名查询书籍
     * @param
     * @return
     */
    @GetMapping("/getBookByBookName")
    public Result getBookByBookName(BookInfoVo bookInfoVo){

        List<BookInfo> result = new ArrayList<>();

        System.out.println(bookInfoVo.getBookName());
        //如果传输过来的查询条件中包含书名则条件查询
        if (!Strings.isNullOrEmpty(bookInfoVo.getBookName())) {
            QueryWrapper<BookInfo> wrapper = new QueryWrapper<>();
            wrapper.like("book_name",bookInfoVo.getBookName());
            result = bookInfoService.list(wrapper);
        }
        System.out.println(result.toString());
        return Result.ok(result);
    }

    /**
     * 查看可借阅数
     * @param bookId
     * @return
     */
    @GetMapping("/getLoanableQuantity/{bookId}")
    public Integer getLoanableQuantity(@PathVariable String bookId){
        BookInfo bookInfo = bookInfoService.getById(bookId);
        if (bookInfo == null){
            return 0;
        }
        return bookInfo.getLoanableQuantity();
    }

    /**
     * 改变可借阅数
     * @param bookInfo
     * @return
     */
    @PostMapping("/changeLoanableQuantity")
    public boolean changeLoanableQuantity(@RequestBody BookInfo bookInfo){
        String bookId = bookInfo.getId();
        BookInfo book = bookInfoService.getById(bookId);
        book.setLoanableQuantity(bookInfo.getLoanableQuantity());
        return bookInfoService.updateById(book);
    }

    @GetMapping("/get/{id}")
    public BookInfo getBook(@PathVariable String id){
        BookInfo bookInfo = bookInfoService.getById(id);

        return bookInfo;
    }
    /**
     * 查询作者
     * @param
     * @return
     */
    @GetMapping("/getBookByAuthor")
    public Result getBookByAuthor(BookInfoVo bookInfoVo){

        List<BookInfo> result = new ArrayList<>();

        //如果传输过来的查询条件中包含作者则条件查询
        if (!Strings.isNullOrEmpty(bookInfoVo.getAuthor())){
            QueryWrapper<BookInfo> wrapper2 = new QueryWrapper<>();
            wrapper2.like("author",bookInfoVo.getAuthor());
            result = bookInfoService.list(wrapper2);
        }

        List<String> list = new ArrayList<>();
        List<String> collect = result.stream().filter(distinctByKey1(s -> s.getAuthor())).map(BookInfo::getAuthor).collect(Collectors.toList());

        List<BookInfoVo> bookInfoVos = new ArrayList<>();
        for (String author : collect){
            bookInfoVo.setAuthor(author);
            bookInfoVos.add(bookInfoVo);
        }
        return Result.ok(bookInfoVos);
    }

    public static <T> Predicate<T> distinctByKey1(Function<? super T, ?> keyExtractor) {
        Map<Object, Boolean> seen = new ConcurrentHashMap<>();
        return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
    }

}
